import { defineStore } from 'pinia'
import { Local } from '@/utils/storage';
export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0,
    isLogin: false,
    user: null,
    token: null,
  }),
  getters: {
    doubleCount(state) {
      return state.count * 2
    },
    doubleCountPlusOne(): number {
      return this.doubleCount + 1
    },
    getLoginStatus(state) {
      return state.isLogin;
    },
  },
  actions: {
    increment() {
      this.count++
    },
    login(user:any, token:any) {
      this.isLogin = true;
      this.user = user;
      this.token = token;
      // 你可以在这里将 token 保存到 localStorage 或 cookie 中
      Local.set('token', token);
      Local.set('user', user);
    },
    logout() {
      this.isLogin = false;
      this.user = null;
      this.token = null;
      // 清除 localStorage 或 cookie 中的 token
      Local.remove('token');
      Local.remove('user');
    },
    initialize() {
      // 初始化时从 localStorage 中恢复登录状态
      const token = Local.get('token');
      const user = Local.get('user');
      if (token) {
        this.isLogin = true;
        this.token = token;
        this.user = user;
        // 这里可以根据需要从 API 获取用户信息
      }
    },
  },
})